//
//  Message_FeedBackVc.swift
//  Pods
//
//  Created by eme on 2017/4/11.
//
//

import UIKit
import UtilCore
import RickonCore

class Message_FeedBackVc: Base_Vc {
    
    /****************************Storyboard UI设置区域****************************/
    @IBOutlet weak var feedback_TableView: UITableView!                 //表
    
    /*----------------------------   自定义属性区域    ----------------------------*/
    
    
    /****************************Storyboard 绑定方法区域****************************/
    var GetFeedBack_DetaileVm:GetFeedBack_Detaile?
    var feedBackId:String?
    var resuleModel:FeedBackDetaileModel?{
        didSet{
            self.feedback_TableView.reloadData()
        }
    }
    
    
    /**************************** 以下是方法区域 ****************************/
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.navigationController?.navigationBar.isHidden = false
        self.GetFeedBack_DetaileVm?.get_complainid.onNext(feedBackId ?? "")
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    /**
     界面基础设置
     */
    override func setupUI() {
        /**
         *  自定义 导航栏左侧 返回按钮
         */
        self.customLeftBarButtonItem()
        self.setUpTableView()
    }
    func setUpTableView(){
        self.feedback_TableView.showsVerticalScrollIndicator = false
        self.feedback_TableView.delegate = self
        self.feedback_TableView.dataSource = self
        self.feedback_TableView.estimatedRowHeight = 100
        self.feedback_TableView.rowHeight = UITableViewAutomaticDimension
    }
    /**
     绑定到viewmodel 设置
     */
    override func bindToViewModel(){
        self.GetFeedBack_DetaileVm = GetFeedBack_Detaile()
        
        self.GetFeedBack_DetaileVm?.result.subscribe(onNext:{ result in
            
            self.resuleModel = result
            
            }).addDisposableTo(disposeBag)
        
        
//        self.GetFeedBack_DetaileVm?.refreshing.asObservable().subscribeNext{ isLoding in
//            if isLoding {
//                
//                self.view.showEmeLoading()
//            }else{
//                
//                self.view.hideEmeLoading()
//            }
//            }.addDisposableTo(disposeBag)
        
        
        self.GetFeedBack_DetaileVm?.error.bindTo(self.view.rx_error).addDisposableTo(disposeBag)
    }
}
extension Message_FeedBackVc:UITableViewDataSource,UITableViewDelegate{
    func numberOfSections(in tableView: UITableView) -> Int {
        if let _ = resuleModel{
            return 3
        }else{
            return 0
        }
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        switch section {
        case 0:
            return 1
        case 1:
            return 1
        case 2:
            return 2
        default:
            return 0
        }
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerCell = tableView.dequeueReusableCell(withIdentifier: "header") as! Message_Feedback_ProgressHeaderCell
        switch section {
        case 0:
            return nil
        case 1:
            headerCell.titleLabel.text = "意见反馈"
            return headerCell
        case 2:
            headerCell.titleLabel.text = "意见反馈进度"
            return headerCell
        default:
            return nil
        }
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let typeCell = tableView.dequeueReusableCell(withIdentifier: "type") as! Message_Feedback_TypeCell
        let contentCell = tableView.dequeueReusableCell(withIdentifier: "content") as! Message_Feedback_ContentCell
        contentCell.item = self.resuleModel
        let progressCell = tableView.dequeueReusableCell(withIdentifier: "progress") as! Message_Feedback_ProgressCell
        if indexPath.row == 0{
            progressCell.Black_View.backgroundColor = UIColor.black
            progressCell.Line_Top.isHidden = true
            progressCell.Line_Bottom.isHidden = false
        }else{
            progressCell.Black_View.backgroundColor = UIColor(hex: "999999")
            progressCell.Line_Bottom.isHidden = true
            progressCell.Line_Top.isHidden = false
        }
        if let model = resuleModel{
            progressCell.time_Label.text = [model.replydate,model.feedbackdate][indexPath.row]
            progressCell.type_Label.text = ["意见反馈已处理","您的意见反馈已提交成功，请等待平台处理"][indexPath.row]
            progressCell.message_Label.text = ["直供宝平台已经处理您的投诉，处理问题：\(model.reply)",UtilCore.alertmsg[100054011]?.msgtitle][indexPath.row]
        }
        

        switch indexPath.section {
        case 0:
            guard #available(iOS 9.0, *) else {
                typeCell.setNeedsUpdateConstraints()
                typeCell.updateConstraintsIfNeeded()
                return typeCell
            }
            return typeCell
        case 1:
            guard #available(iOS 9.0, *) else {
                contentCell.setNeedsUpdateConstraints()
                contentCell.updateConstraintsIfNeeded()
                return contentCell
            }
            return contentCell
        default:
            guard #available(iOS 9.0, *) else {
                progressCell.setNeedsUpdateConstraints()
                progressCell.updateConstraintsIfNeeded()
                return progressCell
            }
            return progressCell
        }
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        switch section {
        case 1,2:
            return 44
        default:
            return 0.1
        }
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 5
    }
}
